package dataObjects;

import java.util.List;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A data access object (DAO) providing persistence and search support for Dose
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see dataObjects.Dose
 * @author MyEclipse Persistence Tools
 */

public class DoseDAO extends BaseHibernateDAO {
	private static final Logger log = LoggerFactory.getLogger(DoseDAO.class);
	// property constants
	public static final String AMOUNT = "amount";
	public static final String UNIT = "unit";
	public static final String FREQUENCY = "frequency";
	public static final String REMINDER = "reminder";

	public void save(Dose transientInstance) {
		log.debug("saving Dose instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Dose persistentInstance) {
		log.debug("deleting Dose instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Dose findById(dataObjects.DoseId id) {
		log.debug("getting Dose instance with id: " + id);
		try {
			Dose instance = (Dose) getSession().get("dataObjects.Dose", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Dose instance) {
		log.debug("finding Dose instance by example");
		try {
			List results = getSession().createCriteria("dataObjects.Dose")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Dose instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Dose as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByAmount(Object amount) {
		return findByProperty(AMOUNT, amount);
	}

	public List findByUnit(Object unit) {
		return findByProperty(UNIT, unit);
	}

	public List findByFrequency(Object frequency) {
		return findByProperty(FREQUENCY, frequency);
	}

	public List findByReminder(Object reminder) {
		return findByProperty(REMINDER, reminder);
	}

	public List findAll() {
		log.debug("finding all Dose instances");
		try {
			String queryString = "from Dose";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Dose merge(Dose detachedInstance) {
		log.debug("merging Dose instance");
		try {
			Dose result = (Dose) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Dose instance) {
		log.debug("attaching dirty Dose instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Dose instance) {
		log.debug("attaching clean Dose instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}